package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
@Service
public class FactoryServiceImpl implements FactoryService {
   @Autowired
    private FactoryDao factoryDao;
    /**
     * 分页查询
     */

    @Override
    public PageInfo<Factory> findByPage(FactoryExample factoryExample, int pageNum, int pageSize) {
        //1.设置分页开始
        PageHelper.startPage(pageNum,pageSize);
        //2.调用方法
        List<Factory> list = factoryDao.selectByExample(factoryExample);
        return new PageInfo<>(list);
    }

    /**
     * 查询所有
     *
     * @param factoryExample
     */
    @Override
    public List<Factory> findAll(FactoryExample factoryExample) {
        return factoryDao.selectByExample(factoryExample);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Factory findById(String id) {
        return factoryDao.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param factory
     */
    @Override
    public void save(Factory factory) {
          factoryDao.insertSelective(factory);
    }

    /**
     * 修改
     *
     * @param factory
     */
    @Override
    public void update(Factory factory) {
         factoryDao.updateByPrimaryKeySelective(factory);
    }

    /**
     * 删除部门
     *
     * @param id
     */
    @Override
    public void delete(String id) {
    factoryDao.deleteByPrimaryKey(id);
    }

    @Override
    public Factory findByFactoryName(String factoryName) {

        //创建example条件对象
        FactoryExample factoryExample = new FactoryExample();
        //构造条件：factoryName作为查询条件
        factoryExample.createCriteria().andFactoryNameEqualTo(factoryName);

        List<Factory> factoryList = factoryDao.selectByExample(factoryExample);

        return (factoryList!=null&& factoryList.size()>0)?factoryList.get(0):null;
    }
}
